home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Converters / Convert_PICT / Source / CommentedPSCode / Bitmaps < prev    next >
Text File  |  1995-06-12  |  8KB  |  336 lines

  1. %BEGIN Bitmaps
  2.  
  3. % Copyright (C) 1993 David John Burrowes
  4. % Distributed under terms of GNU General Public License.
  5. % See COPYING.text in Convert PICT's CommentedPSCode for a copy
  6.  
  7. %%%%%%%%%%%%%
  8. %    Define lengh of a run, and the byte which is repeated that many times
  9. %%%%%%%%%%%%%
  10. /lengthByte 1 string def
  11. /runByte 1 string def
  12.  
  13. %%%%%%%%%%%%%
  14. %    Name:    unpackbytes
  15. %    Syntax:    - unpackbytes -
  16. %    About:    This unpacks packed hex data into a binary string. Data format
  17. %            is identical to PS level 2 RLE and Mac PackBits (plus
  18. %            terminating 0x80).  Each chunk of data starts with a 'length'
  19. %            byte. If it is < 128 use that number of following bytes (plus
  20. %            one) literally. If it is > 128, next byte should be used
  21. %            257-length times.  Note: if 128 ends up in data, we don't deal
  22. %            properly (it shouldn't though)
  23. %%%%%%%%%%%%%
  24. /unpackbytes 
  25.     %
  26.     %    Get a byte which tells us how to extract the next chunk of data
  27.     %
  28.     /thelength 
  29.         currentfile lengthByte readhexstring pop 
  30.         0 get 
  31.     def 
  32.     %
  33.     %    If length < 128, copy that number of bytes+1 in litterally
  34.     %    Otherwise, copy next byte 257-length number of times
  35.     %
  36.     thelength 128 lt 
  37.     { 
  38.         /dataString thelength 1 add  string def 
  39.         currentfile dataString readhexstring pop 
  40.     }
  41.     { 
  42.         /runlength 257 thelength sub def 
  43.         /runString runlength  string def
  44.         currentfile runByte readhexstring pop pop 
  45.  
  46.         0 1 runlength 1 sub 
  47.             { runString exch runByte putinterval } 
  48.         for 
  49.         runString 
  50.     } 
  51.     ifelse 
  52. }
  53. def
  54.  
  55. %%%%%%%%%%%%%
  56. %    Name:    bitmap        [0090, 0091, 0097, 0098  (Pict1 & subset pict2)]
  57. %    Syntax:    colordat bits mode llx lly width height sourcewW sourceH
  58. %            bool [regiondata] bool bitmap -
  59. %    About:    Display a B&W bitmap. If bool is true, clipping region data
  60. %            follows. The width & height of the image, and the rect (origin,
  61. %            width, height) to display it follow. Mac drawing mode, bitdepth,
  62. %            then info for a colorspace definition. Final args are redundant.
  63. %            (both this and colorbitmap have same args this way)
  64. %%%%%%%%%%%%%
  65. /bitmap
  66. {
  67.     gsave
  68.         % use flag on stack
  69.         {
  70.             initclip
  71.             regionBounds
  72.             clip
  73.             /numShapes exch def
  74.             numShapes  0 gt
  75.             {
  76.                 numShapes
  77.                 regionPath
  78.                 clip
  79.             }
  80.             if
  81.         }
  82.         if
  83.         /isPacked exch def
  84.         /sourceHeight exch def
  85.         /sourceWidth exch def
  86.         /destHeight exch def
  87.         /destWidth exch def
  88.         /boty exch def
  89.         /botx exch def
  90.         /mode exch def
  91.         /bitDepth exch def        % unused
  92.         /colordata exch def    %  "
  93.         /numcolors exch def    %  "
  94.         %
  95.         %    Compute a string with length equal to the number of bytes used
  96.         %    per scaline.  That number is (bits-per-scaline /8), & wrap up to
  97.         %    next whole byte.
  98.         %
  99.         /scanString  sourceWidth 8 div ceiling cvi  string def
  100.         botx boty destHeight add translate
  101.         destWidth destHeight scale
  102.         %
  103.         %    With B&W bitmaps, it's easy to support the two Or transfer modes
  104.         %    Check for the two Or modes, and use proper imageMask.
  105.         %    Otherwise, default to doing a srcCpy
  106.         %
  107.         0 setgray
  108.         PSlevel 1 eq
  109.         {
  110.             mode /srcOr eq
  111.             mode /notSrcOr eq
  112.             or 
  113.             {
  114.                 %
  115.                 %    Compute a true or false value for imagemask using mode
  116.                 %
  117.                 sourceWidth sourceHeight     mode /notSrcOr eq
  118.                 [sourceWidth 0 0  sourceHeight 0 sourceHeight] 
  119.                 isPacked true eq
  120.                     { {unpackbytes} }
  121.                     { {currentfile scanString readhexstring pop} } 
  122.                 ifelse
  123.                 imagemask
  124.             }
  125.             {
  126.                 %
  127.                 %    Do a copy mode.
  128.                 %
  129.                 sourceWidth sourceHeight 1
  130.                 [sourceWidth 0 0  sourceHeight 0 sourceHeight] 
  131.                 isPacked true eq
  132.                     { {unpackbytes} }
  133.                     { {currentfile scanString readhexstring pop} } 
  134.                 ifelse
  135.                 image
  136.             }
  137.             ifelse
  138.             %
  139.             %    Remove the trailing end-of-line80 or eol>
  140.             %
  141.             isPacked false eq
  142.                 { 2 { currentfile read pop pop } repeat  }
  143.                 { 4 { currentfile read pop pop } repeat  }
  144.             ifelse
  145.         }
  146.         {
  147.             /DeviceGray  setcolorspace
  148.             %
  149.             %    Build the dict. parameter for the PS2 image or imageMask op.
  150.             %
  151.             /imageParams 7 dict def
  152.             imageParams begin
  153.                 /ImageType            1    def
  154.                 /Width                 sourceWidth  def
  155.                 /Height                sourceHeight  def
  156.                 /BitsPerComponent    1    def
  157.                 /Decode            mode /notSrcOr eq
  158.                                     { [ 1 0 ] }  { [ 0 1 ] } ifelse def
  159.                 /ImageMatrix    [sourceWidth 0 0  sourceHeight
  160.                                     0 sourceHeight]  def
  161.                 /DataSource isPacked true eq
  162.                             { currentfile  /ASCIIHexDecode filter
  163.                                 /RunLengthDecode filter}
  164.                             { currentfile  /ASCIIHexDecode filter}
  165.                             ifelse
  166.                         def
  167.             end
  168.  
  169.             mode /srcOr eq
  170.             mode /notSrcOr eq
  171.             or 
  172.                 { imageParams imagemask }
  173.                 { imageParams image }
  174.             ifelse
  175.         }
  176.         ifelse
  177.     grestore
  178. }
  179. def
  180.  
  181. %%%%%%%%%%%%%
  182. %    Name:    colorbitmap        [0090, 0091, 0097, 0098  (for pict2 images)]
  183. %    Syntax:    colordat bits mode llx lly width height sourcewW sourceH
  184. %            bool [regiondata] bool colorbitmap -
  185. %    About:    Display a bitmap. If bool is true, clipping region data follows.
  186. %            The width and height of the image, and the rect (origin, width,
  187. %            height) to display it follow.  Mac drawing mode, bitdepth, then
  188. %            info for a colorspace definition. The bit data follows.
  189. %%%%%%%%%%%%%
  190. /colorbitmap
  191.     gsave
  192.         % use flag on stack
  193.         {
  194.             initclip
  195.             regionBounds
  196.             clip
  197.             /numShapes exch def
  198.             numShapes  0 gt
  199.             {
  200.                 numShapes
  201.                 regionPath
  202.                 clip
  203.             }
  204.             if
  205.         }
  206.         if
  207.         /isPacked exch def
  208.         /sourceHeight exch def
  209.         /sourceWidth exch def
  210.         /destHeight exch def
  211.         /destWidth exch def
  212.         /boty exch def
  213.         /botx exch def
  214.         /mode exch def
  215.         /sampleDepth  exch def
  216.         /colordata exch def
  217.         /numcolors exch def
  218.         %
  219.         %    Move to the right location and scale coordinate space properly.
  220.         %
  221.         botx boty destHeight add translate
  222.         destWidth destHeight scale
  223.         PSlevel 1 eq
  224.         {
  225.             %
  226.             %    Compute string with space for full scanline of image data:
  227.             %    Use bits per scanline, divide by 8, round up to whole byte.
  228.             %
  229.             /scanString  sourceWidth sampleDepth mul 8 div
  230.                     ceiling cvi  string def
  231.             sourceWidth sourceHeight sampleDepth
  232.             [sourceWidth 0 0  sourceHeight 0 sourceHeight] 
  233.             isPacked true eq
  234.                 { {unpackbytes} }
  235.                 { {currentfile scanString readhexstring pop} } 
  236.             ifelse
  237.             image
  238.             %
  239.             %    Remove trailing end-of-line80 or eol>
  240.             %
  241.             isPacked false eq
  242.                 { 2 { currentfile read pop pop } repeat  }
  243.                 { 4 { currentfile read pop pop } repeat  }
  244.             ifelse
  245.         }
  246.         {
  247.             [ /Indexed  /DeviceRGB numcolors colordata] setcolorspace
  248.             %
  249.             %    Build the dict. parameter to the PS level 2 image operator
  250.             %
  251.             /imageParams 7 dict def
  252.             imageParams begin
  253.                 /ImageType        1 def
  254.                 /Width             sourceWidth def
  255.                 /Height            sourceHeight def
  256.                 /BitsPerComponent    sampleDepth def
  257.                 /Decode            [ 0 2 sampleDepth exp 1 sub ] def
  258.                 /ImageMatrix    [sourceWidth 0 0  sourceHeight 0
  259.                                     sourceHeight]  def
  260.                 /DataSource     isPacked true eq
  261.                             { currentfile  /ASCIIHexDecode filter
  262.                                     /RunLengthDecode filter}
  263.                             { currentfile  /ASCIIHexDecode filter}
  264.                             ifelse
  265.                         def
  266.             end
  267.             imageParams image
  268.         }
  269.         ifelse
  270.     grestore
  271. }
  272. def
  273.  
  274.  
  275. %%%%%%%%%%%%%
  276. %    Name:    24bitBitmap        [009A, 009B]
  277. %    Syntax:    mode llx lly width height sourcewW sourceH
  278. %            [regiondata] bool colorbitmap -
  279. %    About:    This displays a bitmap.  It differs from bitmap and colorbitmap in that
  280. %            the bitmap it is to display is not black and white and requires no color table.
  281. %            It uses only literal 24bit color values for the image.  args are:
  282. %            an optional region definition, the initial width and height, the requested
  283. %            destination width and height, the x and y coord of the lower left, and the mac
  284. %            drawing mode
  285. %%%%%%%%%%%%%
  286. /24bitBitmap
  287.     gsave
  288.         % use flag on stack
  289.         {
  290.             initclip
  291.             regionBounds
  292.             clip
  293.             /numShapes exch def
  294.             numShapes  0 gt
  295.             {
  296.                 numShapes
  297.                 regionPath
  298.                 clip
  299.             }
  300.             if
  301.         }
  302.         if
  303.         /isPacked exch def
  304.         /sourceHeight exch def
  305.         /sourceWidth exch def
  306.         /destHeight exch def
  307.         /destWidth exch def
  308.         /boty exch def
  309.         /botx exch def
  310.         /mode exch def
  311.         %
  312.         %    Move to the right location and scale coordinate space properly.
  313.         %
  314.         botx boty destHeight add translate
  315.         destWidth destHeight scale
  316.         %
  317.         %    Compute string with space for full scanline of image data (convert pixels to bytes)
  318.         %
  319.         /scanString  sourceWidth  3 mul  string def
  320.         sourceWidth sourceHeight 8
  321.         [sourceWidth 0 0  sourceHeight 0 sourceHeight] 
  322.         isPacked true eq
  323.             { {unpackbytes} }
  324.             { {currentfile scanString readhexstring pop} } 
  325.         ifelse
  326.         false 3
  327.         colorimage
  328.     grestore
  329. }
  330. def
  331.  
  332. %END Bitmaps
  333.